<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;

use App\Http\Requests\StoreQuestionRequest;
use App\Repositories\QuestionRepository;

class QuestionController extends Controller
{

	protected $questionRepository;

	function __construct(QuestionRepository $questionRepository)
	{
		//权限验证，也可以写在路由里
		$this->middleware('auth')->except(['index', 'show']);
		$this->questionRepository = $questionRepository;
	}

	/**
	 * Display a listing of the resource.
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function index()
	{
		//
		$questions = $this->questionRepository->getQuestionsFeed();
		return view('question.index', compact('questions'));
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function create()
	{
		//
		return view('question.create');
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @param  \Illuminate\Http\Request $request
	 * @return \Illuminate\Http\Response
	 */
	public function store(StoreQuestionRequest $request)
	{
		$topics = $this->questionRepository->normalizeTopic($request->get('topics'));
		$data = [
			'title' => $request->title,
			'body' => $request->body,
			'user_id' => Auth::id()
		];

		$question = $this->questionRepository->create($data);

		// 建立了多对多关系
		$question->topics()->attach($topics);

		return redirect()->route('question.show', [$question->id]);
	}


	/**
	 * Display the specified resource.
	 *
	 * @param  int $id
	 * @return \Illuminate\Http\Response
	 */
	public function show($id)
	{
		$question = $this->questionRepository->byIdWithTopicsAndAnswers($id);
		return view('question.show', compact('question'));
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int $id
	 * @return \Illuminate\Http\Response
	 */
	public function edit($id)
	{
		//
		$question = $this->questionRepository->byId($id);
		if (Auth::user()->owns($question)) {
			return view('question.edit', compact('question'));
		} else {
			// return Redirect::back();
			return back();
		}

	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  \Illuminate\Http\Request $request
	 * @param  int $id
	 * @return \Illuminate\Http\Response
	 */
	public function update(StoreQuestionRequest $request, $id)
	{
		$topics = $this->questionRepository->normalizeTopic($request->get('topics'));
		$question = $this->questionRepository->byId($id);
		$question->update([
			'title' => $request->title,
			'body' => $request->body
		]);

		$question->topics()->sync($topics);

		return redirect()->route('question.show', [$question->id]);

	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int $id
	 * @return \Illuminate\Http\Response
	 */
	public function destroy($id)
	{
		$question = $this->questionRepository->byId($id);
		if (Auth::user()->owns($question)) {
			$question->delete();
			//$question->topics()->delete();
			return redirect('/');
		}
		abort(403, 'Forbidden'); // return back();
	}

	public function topics(Request $request)
	{
		return \App\Topic::select(['id', 'name'])
			// api/topics?q=lar
			->where('name', 'like', '%' . $request->query('q') . '%')
			->get();
	}
}
